package com.gt.quality.base.pojo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Setter;

import java.io.Serializable;

/**
 * @description: 自定义统一返回json格式
 * @author: makeJava
 * @since JDK21
 **/
@Setter
@Data
public class Resp<T> implements Serializable {

    public static final int SUCCESS = 200;

    public static final int ERROR = 500;

    public static final int NOT_LOGIN = 99998;

    public static final String SUCCESS_STR = "Success";
    public static final String DEFAULT_ERROR = "系统繁忙,请稍后重试";

    //状态码
    @Schema(title = "code", description = "成功返回200")
    private int code;

    //提示信息
    @Schema(title = "msg", description = "提示信息")
    private String msg;

    //返回的数据
    @Schema(title = "data", description = "返回的数据")
    private T data;

    @Schema(title = "count", description = "总数量")
    private long count;

    public Resp(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Resp() {
    }

    public Resp(int code, String msg, T data, long count) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = count;
    }

    /**
     * Description:常用返回数据抽离
     */
    public static <T> Resp<T> ok(String msg, T data) {
        return new Resp<>(SUCCESS, msg, data);
    }

    public static <T> Resp<T> error(String errorMsg, T data) {
        return new Resp<>(ERROR, errorMsg, data);
    }

    public static <T> Resp<T> error(String errorMsg) {
        return new Resp<>(ERROR, errorMsg, null);
    }

    public static <T> Resp<T> fail() {
        return error(DEFAULT_ERROR);
    }

    public static <T> Resp<T> fail(String msg) {
        return error(msg);
    }


    public static <T> Resp<T> respNotLogin() {
        return new Resp<T>(NOT_LOGIN, "登录已过期,请重新登录", null);
    }

    public static <T> Resp<T> ok(T data) {
        return new Resp<T>(SUCCESS, SUCCESS_STR, data);
    }

    public boolean isSuccess() {
        return this.code == SUCCESS;
    }

    public static boolean isSuccess(Resp<?> resp) {
        return resp.code == SUCCESS;
    }

    public static <T> Resp<T> ok() {
        return new Resp<T>(SUCCESS, SUCCESS_STR, null);
    }
}
